package jun.carlauncher;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import app.dinus.com.itemdecoration.GridDividerItemDecoration;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import java.util.List;
import jun.carlauncher.base.BaseActivity;
import jun.carlauncher.utils.AppUtils;
import jun.carlauncher.utils.ListUtils;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by NocodeLee on 2017/10/26.
 * Email：lirizhilirizhi@163.com
 */

public class AppListActivity extends BaseActivity {

  private RecyclerView rvAppList;

  private AppListAdatper appListAdatper;
  private List<AppUtils.AppInfo> appInfoList;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_app_list);

    initView();
  }

  private void initView() {
    rvAppList = (RecyclerView) findViewById(R.id.rv_app_list);
    GridLayoutManager layoutManager = new GridLayoutManager(this, 6);
    rvAppList.setLayoutManager(layoutManager);
  }

  private void getAppInfoList() {
    appListAdatper = new AppListAdatper(R.layout.item_app, appInfoList);
    rvAppList.setAdapter(appListAdatper);

    //分割线
    //GridDividerItemDecoration dividerItemDecoration =
    //    new GridDividerItemDecoration(this, GridDividerItemDecoration.GRID_DIVIDER_VERTICAL);
    //dividerItemDecoration.setVerticalDivider(
    //    getResources().getDrawable(R.drawable.bg_cartoon_divider));
    //dividerItemDecoration.setHorizontalDivider(
    //    getResources().getDrawable(R.drawable.bg_cartoon_divider));
    //rvAppList.addItemDecoration(dividerItemDecoration);


    appListAdatper.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
      @Override public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        AppUtils.launchApp(appInfoList.get(position).getPackageName());
      }
    });
  }

  public class AppListAdatper extends BaseQuickAdapter<AppUtils.AppInfo, BaseViewHolder> {
    public AppListAdatper(int layoutResId, List<AppUtils.AppInfo> data) {
      super(layoutResId, data);
    }

    @Override protected void convert(BaseViewHolder helper, AppUtils.AppInfo item) {
      helper.setText(R.id.tv_app_name, item.getName());
      //helper.setText(R.id.tv_app_name, item.getName()+"-"+item.getPackageName());
      helper.setImageDrawable(R.id.iv_icon, item.getIcon());
    }
  }

  @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
  public void onMessageEvent(List<AppUtils.AppInfo> event) {
    if (ListUtils.getSize(event) != 0) {
      appInfoList = event;
      getAppInfoList();
    } else {
      new GetAPPListTask().execute();
    }
  }

  @SuppressLint("StaticFieldLeak") private class GetAPPListTask
      extends AsyncTask<String, Object, List<AppUtils.AppInfo>> {
    @Override protected List<AppUtils.AppInfo> doInBackground(String... strings) {
      appInfoList = AppUtils.getAppsInfo();
      return appInfoList;
    }

    @Override protected void onPostExecute(List<AppUtils.AppInfo> appInfos) {
      super.onPostExecute(appInfos);
      getAppInfoList();
    }
  }
}
